'''
Created on 2011-11-23

@author: Administrator
'''
from foxtrader import *
import datetime
import os
import shutil  
import zipfile,gzip
def date2int(sdate):
    return sdate.year*10000+sdate.month*100+sdate.day
def str2date(sdate):
    return sdate
    return datetime.date(int(sdate[:4]),int(sdate[4:6]),int(sdate[6:8]))
def int2date(s):
    sdate=str(s)
    if sdate.find('/')<0 and sdate.find('-')<0 :
        return datetime.date(int(sdate[:4]),int(sdate[4:6]),int(sdate[6:8]))
    sdate=sdate.replace('/','-')
    s=[]
    s=sdate.split('-')
    return datetime.date(int(s[0]),int(s[1]),int(s[2]))
class FoxDataBackup():
    def __init__(self):
        if os.path.exists(foxdata_backuppath)==False: os.mkdir(foxdata_backuppath)
    def backup(self,reg):
        srcpath=os.path.join(foxtrader,'data',reg)
        print srcpath
        srcfilename=os.path.join(srcpath,'day.hqd')
        print srcfilename
        if os.path.exists(srcfilename):
            tday=datetime.date.today()
            tyear=tday.year
            tmonth=tday.month
            dstpath=os.path.join(foxdata_backuppath,reg)
            print dstpath
            if os.path.exists(dstpath)==False: 
                os.mkdir(dstpath)
                dstfilename=os.path.join(dstpath,str(tyear*100+tmonth))
                zf=zipfile.ZipFile(dstfilename+'.zip','w')
                zf.write(srcfilename,str(tyear*100+tmonth),zipfile.ZIP_DEFLATED)
                zf.close()
                
                #backup now
                return
            if tmonth in [1,4,7,10]:
                dstfilename=os.path.join(dstpath,str(tyear*100+tmonth))
                zf=zipfile.ZipFile(dstfilename+'.zip','w')
                zf.write(srcfilename,str(tyear*100+tmonth),zipfile.ZIP_DEFLATED)
                zf.close()
                
if __name__ == '__main__':
#    fd=FoxTrader()
#    stockdata=fd.readdayfile('HK')
    starttime=datetime.datetime.now()
    print starttime   
#    downloadandprocessonestock()
    fdb=FoxDataBackup()
    for reg in ['NS','AS','NQ']:
        fdb.backup(reg)

    print 'download OK',starttime,(datetime.datetime.now()-starttime).seconds
    pass